﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using SalaDeEnsayo.ApplicationService.Model.Entity;

namespace SalaDeEnsayo.ApplicationService
{
    public class UsuarioService : BaseService
    {
        public Usuario GetUsuarioByUserName(string userName)
        {
            using (var dr = this.SalaDeEnsayoDB.ExecuteReader("UsuarioListar", userName))
            {
                if (dr.Read())
                {
                    Usuario user = GetEntity(dr);
                    return user;
                }
            }
            return null;
        }

        public Usuario GetUsuarioById(long id)
        {
            using (var dr = this.SalaDeEnsayoDB.ExecuteReader("UsuarioObtenerPorId", id))
            {
                if (dr.Read())
                {
                    Usuario user = GetEntity(dr);
                    return user;
                }
            }
            return null;
        }

        #region [ Private Method ]

        private Usuario GetEntity( IDataReader dr)
        {
            return new Usuario( dr["Id"] is DBNull ? 0:(long) dr["Id"],
                                dr["Nombre"] is DBNull ? string.Empty : (string)dr["Nombre"],
                                dr["Apellido"] is DBNull ? string.Empty : (string)dr["Apellido"],
                                dr["NombreUsuario"] is DBNull ? string.Empty : (string)dr["NombreUsuario"],
                                dr["Documento"] is DBNull ? string.Empty : (string)dr["Documento"],
                                dr["Activo"] is System.DBNull ? false: (bool)dr["Activo"]);
        }

        #endregion
    }
}
